<!DOCTYPE html>

<html lang="en">
  <head>
    <meta charset="utf-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>cup.net.asyn.ioloop &#8212; cup 3.2.33 documentation</title>
    <link rel="stylesheet" type="text/css" href="../../../../_static/pygments.css" />
    <link rel="stylesheet" type="text/css" href="../../../../_static/alabaster.css" />
    <script data-url_root="../../../../" id="documentation_options" src="../../../../_static/documentation_options.js"></script>
    <script src="../../../../_static/doctools.js"></script>
    <script src="../../../../_static/sphinx_highlight.js"></script>
    <link rel="index" title="Index" href="../../../../genindex.html" />
    <link rel="search" title="Search" href="../../../../search.html" />
   
  <link rel="stylesheet" href="../../../../_static/custom.css" type="text/css" />
  
  
  <meta name="viewport" content="width=device-width, initial-scale=0.9, maximum-scale=0.9" />

  </head><body>
  

    <div class="document">
      <div class="documentwrapper">
        <div class="bodywrapper">
          

          <div class="body" role="main">
            
  <h1>Source code for cup.net.asyn.ioloop</h1><div class="highlight"><pre>
<span></span><span class="ch">#!/usr/bin/env python</span>
<span class="c1"># -*- coding: utf-8 -*</span>
<span class="c1"># Copyright: See LICENSE for details.</span>
<span class="c1"># Authors: Guannan Ma (@mythmgn),</span>
<span class="sd">&quot;&quot;&quot;</span>
<span class="sd">:description:</span>
<span class="sd">    pollers for epoll and kqueue and others.</span>
<span class="sd">    Refer IOLoop from tornado:</span>
<span class="sd">        https://www.tornadoweb.org/en/branch2.0/_modules/tornado/ioloop.html</span>
<span class="sd">    Respect to the tornado team:</span>

<span class="sd">Tornado is based on Apache V2.0 License. Here it goes:</span>

<span class="sd">Licensed under the Apache License, Version 2.0 (the &quot;License&quot;); you may</span>
<span class="sd">not use this file except in compliance with the License. You may obtain</span>
<span class="sd">a copy of the License at</span>

<span class="sd">    http://www.apache.org/licenses/LICENSE-2.0</span>

<span class="sd">Unless required by applicable law or agreed to in writing, software</span>
<span class="sd">distributed under the License is distributed on an &quot;AS IS&quot; BASIS, WITHOUT</span>
<span class="sd">WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the</span>
<span class="sd">License for the specific language governing permissions and limitations</span>
<span class="sd">under the License.</span>
<span class="sd">&quot;&quot;&quot;</span>
<span class="kn">import</span> <span class="nn">abc</span>
<span class="kn">import</span> <span class="nn">select</span>

<span class="kn">from</span> <span class="nn">cup</span> <span class="kn">import</span> <span class="n">log</span>
<span class="kn">from</span> <span class="nn">cup</span> <span class="kn">import</span> <span class="n">const</span>
<span class="kn">from</span> <span class="nn">cup</span> <span class="kn">import</span> <span class="n">platforms</span>


<span class="n">IONEW</span> <span class="o">=</span> <span class="mh">0x0</span>
<span class="n">EPOLLIN</span> <span class="o">=</span> <span class="mh">0x001</span>
<span class="n">EPOLLPRI</span> <span class="o">=</span> <span class="mh">0x002</span>
<span class="n">EPOLLOUT</span> <span class="o">=</span> <span class="mh">0x004</span>
<span class="n">EPOLLERR</span> <span class="o">=</span> <span class="mh">0x008</span>
<span class="n">EPOLLHUP</span> <span class="o">=</span> <span class="mh">0x010</span>
<span class="n">EPOLLRDHUP</span> <span class="o">=</span> <span class="mh">0x2000</span>


<span class="n">WRITE</span> <span class="o">=</span> <span class="n">EPOLLOUT</span>
<span class="n">READ</span> <span class="o">=</span> <span class="n">EPOLLIN</span>
<span class="n">ERROR</span> <span class="o">=</span> <span class="n">EPOLLERR</span> <span class="o">|</span> <span class="n">EPOLLHUP</span> <span class="o">|</span> <span class="n">EPOLLRDHUP</span>


<div class="viewcode-block" id="BasePoller"><a class="viewcode-back" href="../../../../cup.net.asyn.html#cup.net.asyn.ioloop.BasePoller">[docs]</a><span class="k">class</span> <span class="nc">BasePoller</span><span class="p">(</span><span class="nb">object</span><span class="p">):</span>
    <span class="n">__metaclass__</span> <span class="o">=</span> <span class="n">abc</span><span class="o">.</span><span class="n">ABCMeta</span>

<div class="viewcode-block" id="BasePoller.fileno"><a class="viewcode-back" href="../../../../cup.net.asyn.html#cup.net.asyn.ioloop.BasePoller.fileno">[docs]</a>    <span class="nd">@abc</span><span class="o">.</span><span class="n">abstractmethod</span>
    <span class="k">def</span> <span class="nf">fileno</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
<span class="w">        </span><span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        return fileno of the poller</span>
<span class="sd">        &quot;&quot;&quot;</span></div>

<div class="viewcode-block" id="BasePoller.register"><a class="viewcode-back" href="../../../../cup.net.asyn.html#cup.net.asyn.ioloop.BasePoller.register">[docs]</a>    <span class="nd">@abc</span><span class="o">.</span><span class="n">abstractmethod</span>
    <span class="k">def</span> <span class="nf">register</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">fd</span><span class="p">,</span> <span class="n">events</span><span class="p">):</span>
<span class="w">        </span><span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        register fd</span>
<span class="sd">        &quot;&quot;&quot;</span></div>

<div class="viewcode-block" id="BasePoller.unregister"><a class="viewcode-back" href="../../../../cup.net.asyn.html#cup.net.asyn.ioloop.BasePoller.unregister">[docs]</a>    <span class="nd">@abc</span><span class="o">.</span><span class="n">abstractmethod</span>
    <span class="k">def</span> <span class="nf">unregister</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">fd</span><span class="p">):</span>
<span class="w">        </span><span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        unregister fd</span>
<span class="sd">        &quot;&quot;&quot;</span></div>

<div class="viewcode-block" id="BasePoller.modify"><a class="viewcode-back" href="../../../../cup.net.asyn.html#cup.net.asyn.ioloop.BasePoller.modify">[docs]</a>    <span class="nd">@abc</span><span class="o">.</span><span class="n">abstractmethod</span>
    <span class="k">def</span> <span class="nf">modify</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">fd</span><span class="p">,</span> <span class="n">events</span><span class="p">):</span>
<span class="w">        </span><span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        modify fd to newmode</span>
<span class="sd">        &quot;&quot;&quot;</span></div>

<div class="viewcode-block" id="BasePoller.poll"><a class="viewcode-back" href="../../../../cup.net.asyn.html#cup.net.asyn.ioloop.BasePoller.poll">[docs]</a>    <span class="nd">@abc</span><span class="o">.</span><span class="n">abstractmethod</span>
    <span class="k">def</span> <span class="nf">poll</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">wait_time</span><span class="p">):</span>
<span class="w">        </span><span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        poll until wait_times passes.</span>
<span class="sd">        &quot;&quot;&quot;</span></div></div>


<div class="viewcode-block" id="Epoller"><a class="viewcode-back" href="../../../../cup.net.asyn.html#cup.net.asyn.ioloop.Epoller">[docs]</a><span class="k">class</span> <span class="nc">Epoller</span><span class="p">(</span><span class="n">BasePoller</span><span class="p">):</span>
<span class="w">    </span><span class="sd">&quot;&quot;&quot;</span>
<span class="sd">    epoll for linux and others</span>
<span class="sd">    &quot;&quot;&quot;</span>
    <span class="k">def</span> <span class="fm">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
<span class="w">        </span><span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">_epoll</span> <span class="o">=</span> <span class="n">select</span><span class="o">.</span><span class="n">epoll</span><span class="p">()</span>

<div class="viewcode-block" id="Epoller.write_params"><a class="viewcode-back" href="../../../../cup.net.asyn.html#cup.net.asyn.ioloop.Epoller.write_params">[docs]</a>    <span class="k">def</span> <span class="nf">write_params</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
<span class="w">        </span><span class="sd">&quot;&quot;&quot;epoll write params&quot;&quot;&quot;</span>
        <span class="k">return</span> <span class="p">(</span><span class="n">select</span><span class="o">.</span><span class="n">EPOLLET</span> <span class="o">|</span> <span class="n">select</span><span class="o">.</span><span class="n">EPOLLOUT</span> <span class="o">|</span> <span class="n">select</span><span class="o">.</span><span class="n">EPOLLERR</span><span class="p">)</span></div>

<div class="viewcode-block" id="Epoller.read_params"><a class="viewcode-back" href="../../../../cup.net.asyn.html#cup.net.asyn.ioloop.Epoller.read_params">[docs]</a>    <span class="k">def</span> <span class="nf">read_params</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
<span class="w">        </span><span class="sd">&quot;&quot;&quot;epoll read params&quot;&quot;&quot;</span>
        <span class="k">return</span> <span class="p">(</span><span class="n">select</span><span class="o">.</span><span class="n">EPOLLET</span> <span class="o">|</span> <span class="n">select</span><span class="o">.</span><span class="n">EPOLLIN</span> <span class="o">|</span> <span class="n">select</span><span class="o">.</span><span class="n">EPOLLERR</span><span class="p">)</span></div>

<div class="viewcode-block" id="Epoller.fileno"><a class="viewcode-back" href="../../../../cup.net.asyn.html#cup.net.asyn.ioloop.Epoller.fileno">[docs]</a>    <span class="k">def</span> <span class="nf">fileno</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
<span class="w">        </span><span class="sd">&quot;&quot;&quot;return fileno of epoll object&quot;&quot;&quot;</span>
        <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">_epoll</span><span class="o">.</span><span class="n">fileno</span><span class="p">()</span></div>

<div class="viewcode-block" id="Epoller.register"><a class="viewcode-back" href="../../../../cup.net.asyn.html#cup.net.asyn.ioloop.Epoller.register">[docs]</a>    <span class="k">def</span> <span class="nf">register</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">fd</span><span class="p">,</span> <span class="n">events</span><span class="p">):</span>
<span class="w">        </span><span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        register events for a fd</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="n">events</span> <span class="o">=</span> <span class="n">events</span> <span class="o">|</span> <span class="n">select</span><span class="o">.</span><span class="n">EPOLLET</span> <span class="o">|</span> <span class="n">select</span><span class="o">.</span><span class="n">EPOLLERR</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">_epoll</span><span class="o">.</span><span class="n">register</span><span class="p">(</span><span class="n">fd</span><span class="p">,</span> <span class="n">events</span><span class="p">)</span></div>

<div class="viewcode-block" id="Epoller.unregister"><a class="viewcode-back" href="../../../../cup.net.asyn.html#cup.net.asyn.ioloop.Epoller.unregister">[docs]</a>    <span class="k">def</span> <span class="nf">unregister</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">fd</span><span class="p">):</span>
<span class="w">        </span><span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        unregister for epoll</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">_epoll</span><span class="o">.</span><span class="n">unregister</span><span class="p">(</span><span class="n">fd</span><span class="p">)</span></div>

<div class="viewcode-block" id="Epoller.modify"><a class="viewcode-back" href="../../../../cup.net.asyn.html#cup.net.asyn.ioloop.Epoller.modify">[docs]</a>    <span class="k">def</span> <span class="nf">modify</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">fd</span><span class="p">,</span> <span class="n">events</span><span class="p">):</span>
<span class="w">        </span><span class="sd">&quot;&quot;&quot;modify kqueue events&quot;&quot;&quot;</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">_epoll</span><span class="o">.</span><span class="n">modify</span><span class="p">(</span><span class="n">fd</span><span class="p">,</span> <span class="n">events</span><span class="p">)</span></div>

<div class="viewcode-block" id="Epoller.poll"><a class="viewcode-back" href="../../../../cup.net.asyn.html#cup.net.asyn.ioloop.Epoller.poll">[docs]</a>    <span class="k">def</span> <span class="nf">poll</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">wait_time</span><span class="p">):</span>
<span class="w">        </span><span class="sd">&quot;&quot;&quot;start to poll&quot;&quot;&quot;</span>
        <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">_epoll</span><span class="o">.</span><span class="n">poll</span><span class="p">(</span><span class="n">wait_time</span><span class="p">)</span></div></div>


<div class="viewcode-block" id="KQueuePoller"><a class="viewcode-back" href="../../../../cup.net.asyn.html#cup.net.asyn.ioloop.KQueuePoller">[docs]</a><span class="k">class</span> <span class="nc">KQueuePoller</span><span class="p">(</span><span class="n">BasePoller</span><span class="p">):</span>
<span class="w">    </span><span class="sd">&quot;&quot;&quot;</span>
<span class="sd">    kqueue for macos</span>
<span class="sd">    &quot;&quot;&quot;</span>
    <span class="k">def</span> <span class="fm">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
<span class="w">        </span><span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">_kq</span> <span class="o">=</span> <span class="n">select</span><span class="o">.</span><span class="n">kqueue</span><span class="p">()</span>

<div class="viewcode-block" id="KQueuePoller.fileno"><a class="viewcode-back" href="../../../../cup.net.asyn.html#cup.net.asyn.ioloop.KQueuePoller.fileno">[docs]</a>    <span class="k">def</span> <span class="nf">fileno</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
<span class="w">        </span><span class="sd">&quot;&quot;&quot;return fileno of kqueue object&quot;&quot;&quot;</span>
        <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">_kq</span><span class="o">.</span><span class="n">fileno</span><span class="p">()</span></div>

<div class="viewcode-block" id="KQueuePoller.register"><a class="viewcode-back" href="../../../../cup.net.asyn.html#cup.net.asyn.ioloop.KQueuePoller.register">[docs]</a>    <span class="k">def</span> <span class="nf">register</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">fd</span><span class="p">,</span> <span class="n">events</span><span class="p">):</span>
<span class="w">        </span><span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        register events for a fd</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="c1"># NOTE: KQ_EV_CLEAR should be set as</span>
        <span class="c1"># it means edge trigger like EPOLLET</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">kvent_control</span><span class="p">(</span><span class="n">fd</span><span class="p">,</span> <span class="n">events</span><span class="p">,</span> <span class="n">select</span><span class="o">.</span><span class="n">KQ_EV_ADD</span> <span class="o">|</span> <span class="n">select</span><span class="o">.</span><span class="n">KQ_EV_CLEAR</span><span class="p">)</span></div>

<div class="viewcode-block" id="KQueuePoller.unregister"><a class="viewcode-back" href="../../../../cup.net.asyn.html#cup.net.asyn.ioloop.KQueuePoller.unregister">[docs]</a>    <span class="k">def</span> <span class="nf">unregister</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">fd</span><span class="p">):</span>
<span class="w">        </span><span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        unregister for kevents</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="n">events</span> <span class="o">=</span> <span class="n">READ</span> <span class="o">|</span> <span class="n">WRITE</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">kvent_control</span><span class="p">(</span><span class="n">fd</span><span class="p">,</span> <span class="n">events</span><span class="p">,</span> <span class="n">select</span><span class="o">.</span><span class="n">KQ_EV_DELETE</span><span class="p">)</span></div>

<div class="viewcode-block" id="KQueuePoller.modify"><a class="viewcode-back" href="../../../../cup.net.asyn.html#cup.net.asyn.ioloop.KQueuePoller.modify">[docs]</a>    <span class="k">def</span> <span class="nf">modify</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">fd</span><span class="p">,</span> <span class="n">events</span><span class="p">):</span>
<span class="w">        </span><span class="sd">&quot;&quot;&quot;modify kqueue events&quot;&quot;&quot;</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">unregister</span><span class="p">(</span><span class="n">fd</span><span class="p">)</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">register</span><span class="p">(</span><span class="n">fd</span><span class="p">,</span> <span class="n">events</span><span class="p">)</span></div>

<div class="viewcode-block" id="KQueuePoller.kvent_control"><a class="viewcode-back" href="../../../../cup.net.asyn.html#cup.net.asyn.ioloop.KQueuePoller.kvent_control">[docs]</a>    <span class="k">def</span> <span class="nf">kvent_control</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">fd</span><span class="p">,</span> <span class="n">events</span><span class="p">,</span> <span class="n">flags</span><span class="p">):</span>
<span class="w">        </span><span class="sd">&quot;&quot;&quot;kevent control&quot;&quot;&quot;</span>
        <span class="n">kevents</span> <span class="o">=</span> <span class="p">[]</span>
        <span class="k">if</span> <span class="n">events</span> <span class="o">&amp;</span> <span class="n">WRITE</span><span class="p">:</span>
            <span class="n">kevents</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">select</span><span class="o">.</span><span class="n">kevent</span><span class="p">(</span>
                    <span class="n">fd</span><span class="p">,</span> <span class="nb">filter</span><span class="o">=</span><span class="n">select</span><span class="o">.</span><span class="n">KQ_FILTER_WRITE</span><span class="p">,</span> <span class="n">flags</span><span class="o">=</span><span class="n">flags</span><span class="p">))</span>
        <span class="k">if</span> <span class="n">events</span> <span class="o">&amp;</span> <span class="n">READ</span> <span class="ow">or</span> <span class="ow">not</span> <span class="n">kevents</span><span class="p">:</span>
            <span class="n">kevents</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">select</span><span class="o">.</span><span class="n">kevent</span><span class="p">(</span>
                    <span class="n">fd</span><span class="p">,</span> <span class="nb">filter</span><span class="o">=</span><span class="n">select</span><span class="o">.</span><span class="n">KQ_FILTER_READ</span><span class="p">,</span> <span class="n">flags</span><span class="o">=</span><span class="n">flags</span><span class="p">))</span>
        <span class="k">for</span> <span class="n">kevent</span> <span class="ow">in</span> <span class="n">kevents</span><span class="p">:</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">_kq</span><span class="o">.</span><span class="n">control</span><span class="p">([</span><span class="n">kevent</span><span class="p">],</span> <span class="mi">0</span><span class="p">)</span></div>

<div class="viewcode-block" id="KQueuePoller.poll"><a class="viewcode-back" href="../../../../cup.net.asyn.html#cup.net.asyn.ioloop.KQueuePoller.poll">[docs]</a>    <span class="k">def</span> <span class="nf">poll</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">wait_time</span><span class="p">):</span>
<span class="w">        </span><span class="sd">&quot;&quot;&quot;kqueue poll&quot;&quot;&quot;</span>
        <span class="n">kevts</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">_kq</span><span class="o">.</span><span class="n">control</span><span class="p">(</span><span class="kc">None</span><span class="p">,</span> <span class="mi">1000</span><span class="p">,</span> <span class="n">wait_time</span><span class="p">)</span>
        <span class="n">events</span> <span class="o">=</span> <span class="p">{}</span>
        <span class="k">for</span> <span class="n">kevent</span> <span class="ow">in</span> <span class="n">kevts</span><span class="p">:</span>
            <span class="n">fd</span> <span class="o">=</span> <span class="n">kevent</span><span class="o">.</span><span class="n">ident</span>
            <span class="k">if</span> <span class="n">kevent</span><span class="o">.</span><span class="n">filter</span> <span class="o">==</span> <span class="n">select</span><span class="o">.</span><span class="n">KQ_FILTER_READ</span><span class="p">:</span>
                <span class="n">events</span><span class="p">[</span><span class="n">fd</span><span class="p">]</span> <span class="o">=</span> <span class="n">events</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="n">fd</span><span class="p">,</span> <span class="mi">0</span><span class="p">)</span> <span class="o">|</span> <span class="n">READ</span>
            <span class="k">if</span> <span class="n">kevent</span><span class="o">.</span><span class="n">filter</span> <span class="o">==</span> <span class="n">select</span><span class="o">.</span><span class="n">KQ_FILTER_WRITE</span><span class="p">:</span>
                <span class="k">if</span> <span class="n">kevent</span><span class="o">.</span><span class="n">flags</span> <span class="o">&amp;</span> <span class="n">select</span><span class="o">.</span><span class="n">KQ_EV_EOF</span><span class="p">:</span>
                    <span class="c1"># if KQ_EV_EOF received, it means the peer has closed</span>
                    <span class="c1"># / refused the socket</span>
                    <span class="n">events</span><span class="p">[</span><span class="n">fd</span><span class="p">]</span> <span class="o">=</span> <span class="n">ERROR</span>
                <span class="k">else</span><span class="p">:</span>
                    <span class="n">events</span><span class="p">[</span><span class="n">fd</span><span class="p">]</span> <span class="o">=</span> <span class="n">events</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="n">fd</span><span class="p">,</span> <span class="mi">0</span><span class="p">)</span> <span class="o">|</span> <span class="n">WRITE</span>
            <span class="k">if</span> <span class="n">kevent</span><span class="o">.</span><span class="n">flags</span> <span class="o">&amp;</span> <span class="n">select</span><span class="o">.</span><span class="n">KQ_EV_ERROR</span><span class="p">:</span>
                <span class="n">events</span><span class="p">[</span><span class="n">fd</span><span class="p">]</span> <span class="o">=</span> <span class="n">events</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="n">fd</span><span class="p">,</span> <span class="mi">0</span><span class="p">)</span> <span class="o">|</span> <span class="n">ERROR</span>
        <span class="k">return</span> <span class="n">events</span><span class="o">.</span><span class="n">items</span><span class="p">()</span></div></div>


<div class="viewcode-block" id="SelectPoller"><a class="viewcode-back" href="../../../../cup.net.asyn.html#cup.net.asyn.ioloop.SelectPoller">[docs]</a><span class="k">class</span> <span class="nc">SelectPoller</span><span class="p">(</span><span class="n">BasePoller</span><span class="p">):</span>
<span class="w">    </span><span class="sd">&quot;&quot;&quot; downgraded to select.select()&quot;&quot;&quot;</span>
    <span class="k">def</span> <span class="fm">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
<span class="w">        </span><span class="sd">&quot;&quot;&quot;not epoll and not kqueue, downgraded to select.select&quot;&quot;&quot;</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">_read_fds</span> <span class="o">=</span> <span class="p">[]</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">_write_fds</span> <span class="o">=</span> <span class="p">[]</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">_error_fds</span> <span class="o">=</span> <span class="p">[]</span>

<div class="viewcode-block" id="SelectPoller.register"><a class="viewcode-back" href="../../../../cup.net.asyn.html#cup.net.asyn.ioloop.SelectPoller.register">[docs]</a>    <span class="k">def</span> <span class="nf">register</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">fd</span><span class="p">,</span> <span class="n">events</span><span class="p">):</span>
        <span class="k">if</span> <span class="n">events</span> <span class="o">&amp;</span> <span class="n">READ</span><span class="p">:</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">read_fds</span><span class="o">.</span><span class="n">add</span><span class="p">(</span><span class="n">fd</span><span class="p">)</span>
        <span class="k">if</span> <span class="n">events</span> <span class="o">&amp;</span> <span class="n">WRITE</span><span class="p">:</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">write_fds</span><span class="o">.</span><span class="n">add</span><span class="p">(</span><span class="n">fd</span><span class="p">)</span>
        <span class="k">if</span> <span class="n">events</span> <span class="o">&amp;</span> <span class="n">ERROR</span><span class="p">:</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">error_fds</span><span class="o">.</span><span class="n">add</span><span class="p">(</span><span class="n">fd</span><span class="p">)</span>
            <span class="c1"># Closed connections are reported as errors by epoll and kqueue,</span>
            <span class="c1"># but as zero-byte reads by select, so when errors are requested</span>
            <span class="c1"># we need to listen for both read and error.</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">read_fds</span><span class="o">.</span><span class="n">add</span><span class="p">(</span><span class="n">fd</span><span class="p">)</span></div>

<div class="viewcode-block" id="SelectPoller.modify"><a class="viewcode-back" href="../../../../cup.net.asyn.html#cup.net.asyn.ioloop.SelectPoller.modify">[docs]</a>    <span class="k">def</span> <span class="nf">modify</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">fd</span><span class="p">,</span> <span class="n">events</span><span class="p">):</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">unregister</span><span class="p">(</span><span class="n">fd</span><span class="p">)</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">register</span><span class="p">(</span><span class="n">fd</span><span class="p">,</span> <span class="n">events</span><span class="p">)</span></div>

<div class="viewcode-block" id="SelectPoller.unregister"><a class="viewcode-back" href="../../../../cup.net.asyn.html#cup.net.asyn.ioloop.SelectPoller.unregister">[docs]</a>    <span class="k">def</span> <span class="nf">unregister</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">fd</span><span class="p">):</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">read_fds</span><span class="o">.</span><span class="n">discard</span><span class="p">(</span><span class="n">fd</span><span class="p">)</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">write_fds</span><span class="o">.</span><span class="n">discard</span><span class="p">(</span><span class="n">fd</span><span class="p">)</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">error_fds</span><span class="o">.</span><span class="n">discard</span><span class="p">(</span><span class="n">fd</span><span class="p">)</span></div>

<div class="viewcode-block" id="SelectPoller.poll"><a class="viewcode-back" href="../../../../cup.net.asyn.html#cup.net.asyn.ioloop.SelectPoller.poll">[docs]</a>    <span class="k">def</span> <span class="nf">poll</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">timeout</span><span class="p">):</span>
        <span class="n">readable</span><span class="p">,</span> <span class="n">writeable</span><span class="p">,</span> <span class="n">errors</span> <span class="o">=</span> <span class="n">select</span><span class="o">.</span><span class="n">select</span><span class="p">(</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">read_fds</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">write_fds</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">error_fds</span><span class="p">,</span> <span class="n">timeout</span>
        <span class="p">)</span>
        <span class="n">events</span> <span class="o">=</span> <span class="p">{}</span>
        <span class="k">for</span> <span class="n">fd</span> <span class="ow">in</span> <span class="n">readable</span><span class="p">:</span>
            <span class="n">events</span><span class="p">[</span><span class="n">fd</span><span class="p">]</span> <span class="o">=</span> <span class="n">events</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="n">fd</span><span class="p">,</span> <span class="mi">0</span><span class="p">)</span> <span class="o">|</span> <span class="n">READ</span>
        <span class="k">for</span> <span class="n">fd</span> <span class="ow">in</span> <span class="n">writeable</span><span class="p">:</span>
            <span class="n">events</span><span class="p">[</span><span class="n">fd</span><span class="p">]</span> <span class="o">=</span> <span class="n">events</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="n">fd</span><span class="p">,</span> <span class="mi">0</span><span class="p">)</span> <span class="o">|</span> <span class="n">WRITE</span>
        <span class="k">for</span> <span class="n">fd</span> <span class="ow">in</span> <span class="n">errors</span><span class="p">:</span>
            <span class="n">events</span><span class="p">[</span><span class="n">fd</span><span class="p">]</span> <span class="o">=</span> <span class="n">events</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="n">fd</span><span class="p">,</span> <span class="mi">0</span><span class="p">)</span> <span class="o">|</span> <span class="n">ERROR</span>
        <span class="k">return</span> <span class="n">events</span><span class="o">.</span><span class="n">items</span><span class="p">()</span></div></div>


<div class="viewcode-block" id="PollerFactory"><a class="viewcode-back" href="../../../../cup.net.asyn.html#cup.net.asyn.ioloop.PollerFactory">[docs]</a><span class="k">class</span> <span class="nc">PollerFactory</span><span class="p">(</span><span class="nb">object</span><span class="p">):</span>
<span class="w">    </span><span class="sd">&quot;&quot;&quot;Poller Factory&quot;&quot;&quot;</span>

    <span class="k">def</span> <span class="fm">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
<span class="w">        </span><span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">_polldict</span> <span class="o">=</span> <span class="p">[]</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">_poller</span> <span class="o">=</span> <span class="kc">None</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">_started</span> <span class="o">=</span> <span class="kc">False</span>
        <span class="k">if</span> <span class="nb">hasattr</span><span class="p">(</span><span class="n">select</span><span class="p">,</span> <span class="s1">&#39;epoll&#39;</span><span class="p">):</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">_poller</span> <span class="o">=</span> <span class="n">Epoller</span><span class="p">()</span>
        <span class="k">elif</span> <span class="nb">hasattr</span><span class="p">(</span><span class="n">select</span><span class="p">,</span> <span class="s1">&#39;kqueue&#39;</span><span class="p">):</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">_poller</span> <span class="o">=</span> <span class="n">KQueuePoller</span><span class="p">()</span>
        <span class="k">else</span><span class="p">:</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">_poller</span> <span class="o">=</span> <span class="n">SelectPoller</span><span class="p">()</span>

<div class="viewcode-block" id="PollerFactory.stop"><a class="viewcode-back" href="../../../../cup.net.asyn.html#cup.net.asyn.ioloop.PollerFactory.stop">[docs]</a>    <span class="k">def</span> <span class="nf">stop</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
<span class="w">        </span><span class="sd">&quot;&quot;&quot;stop the poller factory&quot;&quot;&quot;</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">_started</span> <span class="o">=</span> <span class="kc">False</span></div>

<div class="viewcode-block" id="PollerFactory.register"><a class="viewcode-back" href="../../../../cup.net.asyn.html#cup.net.asyn.ioloop.PollerFactory.register">[docs]</a>    <span class="k">def</span> <span class="nf">register</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">fd</span><span class="p">,</span> <span class="n">events</span><span class="p">):</span>
<span class="w">        </span><span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        register</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">_poller</span><span class="o">.</span><span class="n">register</span><span class="p">(</span><span class="n">fd</span><span class="p">,</span> <span class="n">events</span><span class="p">)</span></div>

<div class="viewcode-block" id="PollerFactory.modify"><a class="viewcode-back" href="../../../../cup.net.asyn.html#cup.net.asyn.ioloop.PollerFactory.modify">[docs]</a>    <span class="k">def</span> <span class="nf">modify</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">fd</span><span class="p">,</span> <span class="n">events</span><span class="p">):</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">_poller</span><span class="o">.</span><span class="n">modify</span><span class="p">(</span><span class="n">fd</span><span class="p">,</span> <span class="n">events</span><span class="p">)</span></div>

<div class="viewcode-block" id="PollerFactory.unregister"><a class="viewcode-back" href="../../../../cup.net.asyn.html#cup.net.asyn.ioloop.PollerFactory.unregister">[docs]</a>    <span class="k">def</span> <span class="nf">unregister</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">fd</span><span class="p">):</span>
<span class="w">        </span><span class="sd">&quot;&quot;&quot;unregister&quot;&quot;&quot;</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">_poller</span><span class="o">.</span><span class="n">unregister</span><span class="p">(</span><span class="n">fd</span><span class="p">)</span></div>

<div class="viewcode-block" id="PollerFactory.poll"><a class="viewcode-back" href="../../../../cup.net.asyn.html#cup.net.asyn.ioloop.PollerFactory.poll">[docs]</a>    <span class="k">def</span> <span class="nf">poll</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">wait_time</span><span class="p">):</span>
<span class="w">        </span><span class="sd">&quot;&quot;&quot;poll&quot;&quot;&quot;</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">_started</span> <span class="o">=</span> <span class="kc">True</span>
        <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">_poller</span><span class="o">.</span><span class="n">poll</span><span class="p">(</span><span class="n">wait_time</span><span class="p">)</span></div></div>


<span class="c1"># vi:set tw=0 ts=4 sw=4 nowrap fdm=indent</span>
</pre></div>

          </div>
          
        </div>
      </div>
      <div class="sphinxsidebar" role="navigation" aria-label="main navigation">
        <div class="sphinxsidebarwrapper">
<h1 class="logo"><a href="../../../../index.html">cup</a></h1>








<h3>Navigation</h3>
<p class="caption" role="heading"><span class="caption-text">Contents:</span></p>
<ul>
<li class="toctree-l1"><a class="reference internal" href="../../../../cup.html">cup package</a></li>
</ul>

<div class="relations">
<h3>Related Topics</h3>
<ul>
  <li><a href="../../../../index.html">Documentation overview</a><ul>
  <li><a href="../../../index.html">Module code</a><ul>
  <li><a href="../../net.html">cup.net</a><ul>
  </ul></li>
  </ul></li>
  </ul></li>
</ul>
</div>
<div id="searchbox" style="display: none" role="search">
  <h3 id="searchlabel">Quick search</h3>
    <div class="searchformwrapper">
    <form class="search" action="../../../../search.html" method="get">
      <input type="text" name="q" aria-labelledby="searchlabel" autocomplete="off" autocorrect="off" autocapitalize="off" spellcheck="false"/>
      <input type="submit" value="Go" />
    </form>
    </div>
</div>
<script>document.getElementById('searchbox').style.display = "block"</script>








        </div>
      </div>
      <div class="clearer"></div>
    </div>
    <div class="footer">
      &copy;2023, CUP-DEV Team.
      
      |
      Powered by <a href="http://sphinx-doc.org/">Sphinx 7.0.1</a>
      &amp; <a href="https://github.com/bitprophet/alabaster">Alabaster 0.7.13</a>
      
    </div>

    

    
  </body>
</html>